package com.zhangmeng.tools.utils;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class FontUtils {

    public static void main(String[] args) {
        init();

        String[] family = getFamily();
        for (String s : family) {
            String[] fontStyles = getFontStyles(s);
            for (String fontStyle : fontStyles) {
                System.out.println(s +":" + fontStyle);
            }
        }
    }

    private static Font[] fonts;

    public static void init(){
        fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
    }

    private static String calculateStyle(Font font, String fontFamily) {
        StringBuffer style = new StringBuffer(font.getFontName().replace(fontFamily, ""));
        if (style.toString().startsWith(" ")) {
            style.replace(0, 1, "");
        }
        return style.toString();
    }

    private static String[] getFontStyles(String fontFamily) {
        List fontStyles = new ArrayList();
        for (int i = 0; i < fonts.length; i++) {
            Font f = fonts[i];
            if (f.getFamily().equals(fontFamily)) {
                if (f.getFontName().length() == fontFamily.length()) {
                    fontStyles.add("Plain");
                    continue;
                }
                fontStyles.add(calculateStyle(f, fontFamily));
            }
        }
        String[] str = new String[fontStyles.size()];
        for (int i = 0; i < str.length; i++) {
            str[i] = fontStyles.get(i).toString();
        }
        return str;
    }

    private static String[] getFamily() {
        Set stylesSet = new TreeSet();
        for (int i = 0; i < fonts.length; i++) {
            stylesSet.add(fonts[i].getFamily());
        }
        Object[] obj = stylesSet.toArray();
        String[] str = new String[obj.length];
        for (int i = 0; i < str.length; i++) {
            str[i] = obj[i].toString();
        }
        return str;
    }

}
